package com.honor.games.wg.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.swing.JTextField;
import javax.swing.JTextPane;
import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.SimpleAttributeSet;
import javax.swing.text.StyledDocument;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.eventbus.EventBus;
import com.honor.games.wg.events.NewCommandEvent;

public class DebugFrameActionListener implements ActionListener {

	private static Logger logger = LoggerFactory.getLogger(DebugFrameActionListener.class);
	
	private EventBus eventBus;
	
	private JTextField commandField;

	private JTextPane commandTextPane;
	
	public DebugFrameActionListener(JTextField commandField, JTextPane commandTextPane, EventBus eventBus) {
		this.commandField = commandField;
		this.commandTextPane = commandTextPane;
		this.eventBus = eventBus;
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {	
		logger.trace("Command Click");
		StyledDocument doc = commandTextPane.getStyledDocument();
		String text = commandField.getText();
		StringBuilder builder = new StringBuilder();
		Date date = new Date();
		DateFormat df = new SimpleDateFormat("[dd/MM/yyyy-hh:mm:ss]");
		builder.append(df.format(date));
		builder.append(" ");
		builder.append(text);
		builder.append("\n");
		try {
			doc.insertString(0, builder.toString(), new SimpleAttributeSet());
		} catch (BadLocationException e1) {
			e1.printStackTrace();
		}
		eventBus.post(new NewCommandEvent(text));
		commandField.setText("");
	}

}
